
var express = require("express");

var Router = express.Router();  //创建一个路由

var { query } = require("../tool/conn.js");

// Router.get(routerPath,callback)   以get方式对路由进行拦截
// Router.post(routerPath,callback)  以post方式对路由进行拦截
// Router.all(routerPath,callback)   以所有方式对路由进行拦截

// routerPath 路由地址  可以是一级路由,也可以是二级路由
// callback   路由被访问时执行的回调函数

Router.get("/", function (req, res) {
    console.log("/");
    res.send("/car");
})

Router.post("/add", function (req, res) {
    var data = req.body;
    var { user, gid, buyNum } = data;
    if (user && gid && buyNum) {

        var searchSql = `select * from shoppingcar where user = '${user}' and gid = ${gid}`

        query(searchSql).then(list => {
            if (list.length > 0) { // 买过 => 更新
                var sql = `update shoppingcar set buyNum = buyNum + ${buyNum}   where user = '${user}' and gid = ${gid}`
            } else {//没买过 =>  新增
                var sql = `insert into shoppingcar(user,gid,buyNum)  values('${user}',${gid},${buyNum})`
            }

            query(sql).then(info => {
                var { affectedRows } = info;
                if (affectedRows > 0) {
                    res.json({
                        status: true,
                        message: "加入成功",
                    })
                } else {
                    res.json({
                        status: false,
                        message: "语句执行成功,数据未改变",
                    })
                }

            }).catch(err => {
                res.json({
                    status: false,
                    message: err.message
                })
            })
        }).catch(err => {
            res.json({
                status: false,
                message: err.message
            })
        })


        // 无脑新增 => 新增之前先判断 用户是否购买过该商品 
        // 没买过 => 新增
        // 买过  => 更新




    } else {
        res.json({
            status: false,
            message: "请传入完整参数",
        })
    }

})
Router.post("/search", function (req, res) {
    var { user } = req.body;
    if (user) {
        var searchSql = `
            select
                s.id,s.user,s.gid,s.buyNum,g.name,g.img,g.price 
            from
                shoppingcar as s
                join goodslist as g
            on s.gid = g.id 
            where
                s.user = '${user}'`

        query(searchSql).then(list => {
            if (list.length > 0) {
                res.status(200).json({
                    status: true,
                    message: "OK",
                    list: list,
                })
            } else {
                res.status(200).json({
                    status: true,
                    message: "该用户还未购买任何商品",
                    list: [],
                })
            }
        }).catch(err => {
            res.status(500).json({
                status: false,
                message: err.message
            })
        })
    } else {
        res.status(200).json({
            status: false,
            message: "请传入完整参数"
        })
    }
})


Router.post("/update", function (req, res) {
    var { id, type, buyNum } = req.body;
    if (isNaN(id) || id < 0 || id % 1 != 0) {
        return res.json({
            status: false,
            message: "id需要是一个正整数"
        })
    }
    var typeList = ["0", "1", "2"]
    if (!typeList.includes(type)) {
        return res.json({
            status: false,
            message: "type只能是0,1,2"
        })
    }

    if (isNaN(buyNum) || buyNum < 0 || buyNum % 1 != 0) {
        return res.json({
            status: false,
            message: "buyNum需要是一个正整数"
        })
    }
    if (id && type && buyNum) {
        if (type == 0) {
            var sql = `update shoppingcar set buyNum = buyNum - ${buyNum}   where  id = ${id}`;
        } else if (type == 1) {
            var sql = `update shoppingcar set buyNum = buyNum + ${buyNum}   where  id = ${id}`;
        } else if (type == 2) {
            var sql = `update shoppingcar set buyNum = ${buyNum}   where  id = ${id}`;
        }

        query(sql).then(result => {
            var { affectedRows } = result;
            if (affectedRows > 0) {
                res.json({
                    status: true,
                    message: "更新成功",
                })
            } else {
                res.json({
                    status: true,
                    message: "语句成功,但是数据未改变",
                })
            }
        }).catch(err => {
            res.json({
                status: false,
                message: err.message,
            })
        })



    }
})


Router.get("/delete", function (req, res) {
    // id=1
    // id=1,2,3
    var { id } = req.query;
    console.log(id);
    if (id) {
        var deleteSql = `delete from shoppingcar where id in (${id})`;
        query(deleteSql).then(result => {
            var { affectedRows } = result;
            if (affectedRows > 0) {
                res.json({
                    status: true,
                    message: "删除成功",
                })
            } else {
                res.json({
                    status: true,
                    message: "删除数据不存在",
                })
            }
        }).catch(err => {
            res.json({
                status: false,
                message: err.message,
            })
        })
    } else {
        res.send({
            status: false,
            message: "请传入完整参数"
        })
    }

})


module.exports = Router;  //暴露创建的路由配置 (还需要配置到创建的服务中)

